package com.jinmdz.fmis.dao.admin.dao.provider;

import com.jinmdz.fmis.dao.admin.model.role.RoleListData;
import com.jinmdz.fmis.dao.base.BaseProvider;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.jdbc.SQL;

/**
 * 业务角色维护Provider
 *
 * @author LiCongLu
 * @date 2020-03-18 11:36
 */
public class RoleProvider extends BaseProvider {

    /**
     * 查询所有业务角色信息
     *
     * @param data 查询条件
     * @return
     * @author LiCongLu
     * @date 2020-03-18 11:36
     */
    public String loadRoleListWithBusinessSystemId(@Param("data") RoleListData data) {
        return new SQL() {
            {
                StringBuilder builder = new StringBuilder();
                builder.append(" business_system_role.id,business_system_id,role_code,role_name,role_type_code");
                builder.append(" ,business_system_role.disable_state,business_system_role.sort,business_system_role.remark,business_system_role.version ");
                builder.append(" ,business_system.system_name as business_system_name");
                SELECT(builder.toString());
                FROM(" business_system_role ");
                JOIN(" business_system on business_system.id = business_system_id ");
                WHERE(" business_system_role.deleted = 0 and business_system.deleted = 0 ");

                // 验证业务系统主键
                if (valid(data.getBusinessSystemId())) {
                    WHERE(" business_system_id = #{data.businessSystemId} ");
                }

                // 验证角色名称
                if (valid(data.getRoleName())) {
                    WHERE(" role_name like concat('%',#{data.roleName},'%') ");
                }

                // 判断角色分类
                if (validAll(data.getRoleTypeCode())) {
                    WHERE(" role_type_code = #{data.roleTypeCode} ");
                }

                // 判断有效状态
                if (validAll(data.getDisableState())) {
                    WHERE(" business_system_role.disable_state = #{data.disableState} ");
                }

                ORDER_BY(" business_system_role.sort ");
            }
        }.toString();
    }

    /**
     * 按照主键获取业务角色信息
     *
     * @param id 主键
     * @return
     * @author LiCongLu
     * @date 2020-03-18 11:52
     */
    public String getRoleEditById(@Param("id") Integer id) {
        return new SQL() {
            {
                StringBuilder builder = new StringBuilder();
                builder.append(" business_system_role.id,business_system_id,role_code,role_name,role_type_code");
                builder.append(" ,business_system_role.disable_state,business_system_role.sort,business_system_role.remark,business_system_role.version ");
                builder.append(" ,business_system.system_name as business_system_name");
                SELECT(builder.toString());
                FROM(" business_system_role ");
                JOIN(" business_system on business_system.id = business_system_id ");
                WHERE(" business_system_role.deleted = 0 and business_system.deleted = 0 ");
                WHERE(" business_system_role.id = #{id}");
                ORDER_BY(" business_system_role.id limit 1 ");
            }
        }.toString();
    }
}
